package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 订单任务
 *
 * @author liudo
 * @version 1.0
 * @project sky-take-out
 * @date 2024/2/27 09:08:07
 */
@Component
@AllArgsConstructor
public class OrderTask {

    private OrderMapper orderMapper;

    /**
     * 订单超时未支付，状态变更为 6
     */
    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟去处理一次 9.55 9.56
    // @Scheduled(fixedRate = 60000, timeUnit = TimeUnit.MINUTES)
    public void autoCancel() {
        LocalDateTime minusMinutes = LocalDateTime.now().minusMinutes(15);
        orderMapper.updateByStatusBeforeTime(Orders.PENDING_PAYMENT, minusMinutes);
    }

    // 用户收货后管理端未点击完成按钮，订单一直处于派送中状态
    // 每天凌晨 2点来执行这个任务
    @Scheduled(cron = "0 0 2 * * ?")
    public void completeOrder() {
        orderMapper.updateStatusByStatus(Orders.COMPLETED, Orders.DELIVERY_IN_PROGRESS);
    }
}
